<script setup lang="ts">
import { getEnterPriseChildList } from '@/apis';
import { useEnterPrise } from '@/composition/enterprise';
import type { Enterprise } from '@/types/enterprise';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

// Vue3极致的封装就是一堆use-把逻辑都封装起来
const router = useRouter()
const { list, queryParams, changePage, totalNum } = useEnterPrise()

const keys = ref<string[]>([]) // 用来记录所有展开的key
// refreshQuery 带条件的查询

// 每一行的数据
const expandRow = async (params: Enterprise, rows: Enterprise[]) => {
  const rowId = params.id.toString()

  // 如果是展开
  if (rows.some(item => item.id === params.id)) {
    // 检查是否已经加载过子数据
    const childList = await getEnterPriseChildList(rowId)
    params.children = childList
    // 展开把id放入到keys中
    keys.value.push(params.id.toString())
  } else {
    // 折叠把id删除
    keys.value = keys.value.filter(id => id !== params.id.toString())
  }
}
// 
const getStatusLabel = (status: number) => {
  switch (status) {
    case 0:
      return "待生效"
    case 1:
      return "生效中"
    case 2:
      return "已到期"
    default:
      return "已退租"
  }

}
const getStatusType = (status: number) => {
  switch (status) {
    case 0:
      return "primary"
    case 1:
      return "success"
    case 2:
      return "danger"
    default:
      return "info"
  }

}

</script>

<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input v-model.trim="queryParams.name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="changePage(1)">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="router.push('/park/parkDetail')">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :expand-row-keys="keys" row-key="id" @expand-change="expandRow" style="width: 100%" :data="list">
        <el-table-column type="expand">
          <!-- 放置表格 -->
          <template #default="{ row }">
            <div class="child_table">
              <el-table :data="row.children">
                <el-table-column prop="buildingName" label="租赁楼宇"></el-table-column>
                <el-table-column label="租赁起止时间">
                  <template #default="obj">
                    {{ obj.row.startTime }} - {{ obj.row.endTime }}
                  </template>
                </el-table-column>
                <el-table-column prop="status" label="合同状态">
                  <template #default="obj">
                    <el-tag :type="getStatusType(obj.row.status)"> {{ getStatusLabel(obj.row.status) }}</el-tag>
                  </template>

                </el-table-column>
                <el-table-column label="操作">
                  <template #default="obj">
                    <el-button :disabled="!obj.row.renewFlag" type="text">续租</el-button>
                    <el-button :disabled="!obj.row.exitFlag" type="text">退租</el-button>
                    <el-button :disabled="obj.row.status !== 3" type="text">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>

          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="120" />
        <el-table-column prop="name" label="企业名称" width="320" />
        <el-table-column prop="contact" label="联系人" />
        <el-table-column prop="contactNumber" label="联系电话" />
        <el-table-column label="操作" width="300">
          <template #default="{ row }">
            <el-button size="small" type="text">添加合同</el-button>
            <el-button size="small" type="text">查看</el-button>
            <el-button size="small" type="text" @click="router.push(`/park/parkDetail/${row.id}`)">编辑</el-button>
            <el-button size="small" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination @current-change="changePage" :page-size="queryParams.pageSize" :current-page="queryParams.page"
        :total="totalNum" layout="total, prev, pager, next" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.building-container {
  padding: 20px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {

  padding: 4px 0px;
  display: flex;
  justify-content: end;
}

.form-container {
  padding: 0px 80px;
}

.child_table {
  padding-left: 100px;
}
</style>
